﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using Core.DAL;
using Core.Utils;

namespace Core.BOL
{
    public class ImagesRepository
    {
        private ScoutNetDataContext context = null;

        #region Construtor
        public ImagesRepository()
        {
            context = GeneralUtils.GetDataContext();
        }
        #endregion

        #region Methodes
        public void InsertImage(tSite_Photos_Image image)
        {
            context.tSite_Photos_Images.InsertOnSubmit(image);
        }

        public int GetAlbumIDByImageID(int IID)
        {
            var query = (from i in context.tSite_Photos_Images
                         where i.ID == IID
                         select new { i.AID }).SingleOrDefault();

            if (query != null)
            {
                return query.AID;
            }
            return -1;
        }

        public List<sp_Site_GetRandomImageBySystemNameResult> GetRandomImageBySystemName(int numItem, string systemName)
        {
            return context.sp_Site_GetRandomImageBySystemName(numItem, systemName).ToList();
        }

        public List<sp_Site_GetRandomImageResult> GetRandomImage(int numItem)
        {
            return context.sp_Site_GetRandomImage(numItem).ToList();
        }

        public List<sp_Site_GetRandomUserImageResult> GetRandomUserImage(int numItem, Guid userID)
        {
            return context.sp_Site_GetRandomUserImage(numItem, userID).ToList();
        }

        public List<tSite_Photos_Image> GetImagesByAlbumID(int albumID, int skip, int take)
        {
            var query = (from i in context.tSite_Photos_Images
                         join a in context.tSite_Photos_Albums on i.AID equals a.ID
                         orderby i.ID
                         where i.AID == albumID
                         select i).Skip(skip).Take(take).ToList();

            return query;
        }

        public int GetCountImages(string sectionName)
        {
            int query = (from i in context.tSite_Photos_Images
                         join s in context.tSite_Sections on i.SID equals s.ID
                         where s.SystemName == sectionName
                         select i).Count();
            return query;
        }

        public int GetCountImagesByAlbum(int AID)
        {
            int query = (from i in context.tSite_Photos_Images
                         where i.AID == AID
                         select i).Count();

            return query;
        }

        public tSite_Photos_Image GetImageByID(int ID)
        {
            var query = (from i in context.tSite_Photos_Images
                         join a in context.tSite_Photos_Albums on i.AID equals a.ID
                         where i.ID == ID
                         select i).SingleOrDefault();

            return query;
        }

        public bool IsLatestImageOfAlbum(int IID)
        {
            int countItem = (from i in context.tSite_Photos_Images
                             join a in context.tSite_Photos_Albums on i.AID equals a.ID
                             where a.ID == context.tSite_Photos_Images.Where(p=> p.ID == IID).Single().AID
                             select i).Count();

            return (countItem == 1);
        }

        public bool IsDefaultImageOfAlbum(int IID)
        {
            return (context.tSite_Photos_Albums.Where(p => p.DefaultImage == IID).Count() > 0);
        }

        public List<tSite_Photos_Image> GetPreviousImage(int IID, int take, int AID)
        {
            var query = (from n in context.tSite_Photos_Images
                         where n.ID < IID && n.AID == AID
                         orderby n.ID descending
                         select n).Take(take);
            return query.ToList();
        }

        public List<tSite_Photos_Image> GetNextImage(int IID, int take, int AID)
        {
            var query = (from n in context.tSite_Photos_Images
                         where n.ID > IID && n.AID == AID
                         orderby n.ID ascending
                         select n).Take(take);
            return query.ToList();
        }

        public void DeleteImagesByAlbumID(int AID)
        {
            // Delete comments
            CommentRepository cr = new CommentRepository();
            cr.DeletePictureCommentsByAlbumID(AID);
            cr.Save();

            //Delete MyAlbum Images
            MyAlbumRepository mar = new MyAlbumRepository();
            mar.DeleteImagesByAlbumID(AID);
            mar.Save();

            // Delete rating
            RatingRepository rr = new RatingRepository();
            rr.DeletePictureRatingsByAlbumID(AID);
            rr.Save();

            //Delete Picture
            List<tSite_Photos_Image> li = context.tSite_Photos_Images.Where(p => p.AID == AID).ToList();

            if (li != null && li.Count > 0)
            {
                foreach (tSite_Photos_Image i in li)
                {
                    string imagePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["GalleryPath"] + i.AID.ToString("D6") + "/" + i.ImageName + ".jpg");
                    if (File.Exists(imagePath))
                    {
                        // Delete original picture
                        Imaging.DeleteImageCached(i.ID);

                        // Delete original picture
                        File.Delete(imagePath);
                    }    
                }
                
                context.tSite_Photos_Images.DeleteAllOnSubmit(li);
            }
        }

        public void DeletePictureByImageID(int IID)
        {
            // Delete comment
            CommentRepository cr = new CommentRepository();
            cr.DeletePictureCommentsByImageID(IID);
            cr.Save();

            //Delete MyAlbum Images
            MyAlbumRepository mar = new MyAlbumRepository();
            mar.DeleteImagesByImageID(IID);
            mar.Save();

            // Delete rating
            RatingRepository rr = new RatingRepository();
            rr.DeletePictureRatingsByImageID(IID);
            rr.Save();

            //Delete Picture
            tSite_Photos_Image i = context.tSite_Photos_Images.Where(p => p.ID == IID).SingleOrDefault();

            if (i != null)
            {
                string imagePath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["GalleryPath"] + i.AID.ToString("D6") + "/" + i.ImageName + ".jpg");

                if (File.Exists(imagePath))
                {
                    // Delete original picture
                    File.Delete(imagePath);
                    
                    // Delete original picture
                    Imaging.DeleteImageCached(i.ID);
                    
                }
                context.tSite_Photos_Images.DeleteOnSubmit(i);
            }
        }

        public void Save()
        {
            context.SubmitChanges();
        }
        #endregion
    }
}
